package com.ruoyi.ring.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;

public class ArrayToStringDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        // 解析JSON数组并转换为逗号分隔的字符串
        JsonNode node = p.getCodec().readTree(p);
        if (node.isArray()) {
            StringBuilder sb = new StringBuilder();
            for (JsonNode element : node) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(element.asText());
            }
            return sb.toString();
        }
        // 如果不是数组，直接返回字符串
        return node.asText();
    }
}